<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar title="编辑资料" left-arrow @click-left="$router.back()" fixed />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image round class="avatar" :src="profileObj.photo" @click="imageClickFn" />
          <!-- file 选择框 -->
          <input type="file" ref="iptFile" v-show="false" accept="image/*" @change="onFileChange" />
        </template>
      </van-cell>
      <van-cell title="名称" is-link :value="profileObj.name" @click="nameClickFn" />
      <van-cell title="生日" is-link :value="profileObj.birthday" @click="birthdayClickFn"/>
    </van-cell-group>

    <!-- 姓名修改弹窗 -->
    <van-dialog v-model="show" title="标题" show-cancel-button :before-close="beforeCloseFn">
      <input type="text" v-fofo v-model.trim="inputUserName" />
    </van-dialog>

    <!-- 时间选择器 -->
    <van-popup v-model="dateTimeShow" position="bottom" :style="{ height: '50%' }" round>
      <van-datetime-picker v-model="currentDate" type="date" title="选择年月日" :min-date="minDate" :max-date="maxDate" @cancel="dataTimeCancelFn" @confirm="confirmFn"/>
    </van-popup>
  </div>
</template>

<script>
import { userProfileAPI, updateUserPhotoAPI, updateUserProfileAPI } from '@/api/index.js'
import { formatDate } from '@/utils/date.js'
import { mapMutations } from 'vuex'
import Notify from '@/ui/Notify.js'

export default {
  name: 'UserEdit',
  data () {
    return {
      profileObj: {}, // 用户基本资料
      show: false, // 控制姓名修改输入框的显示与隐藏
      inputUserName: '', // 修改姓名绑定的值
      minDate: new Date(1920, 0, 1), // 最小值
      maxDate: new Date(), // 最大值
      currentDate: new Date(), // 显示值
      dateTimeShow: false // 日期选择框的显示
    }
  },
  async created () {
    const { data: res } = await userProfileAPI()
    this.profileObj = res.data
  },
  methods: {
    ...mapMutations(['SET_USERPHOTO']),

    // 文件选择改变事件
    async onFileChange (e) {
      // 如果用户没有选择图片
      if (e.target.files.length === 0) return
      // 创建FormaDate对象
      const theFd = new FormData()
      theFd.append('photo', e.target.files[0])
      const { data: res } = await updateUserPhotoAPI(theFd)
      this.profileObj.photo = res.data.photo
      this.SET_USERPHOTO(res.data.photo)
    },
    // 图片-点击事件
    imageClickFn () {
      // js模拟点击效果
      this.$refs.iptFile.click()
    },
    // 修改用户名
    nameClickFn () {
      this.show = !this.show
      this.inputUserName = this.profileObj.name
    },
    // 点击修改用户名确定前的回调函数
    async beforeCloseFn (action, done) {
      if (action === 'confirm') {
        // 点击的是确定按钮
        const reg = /^[a-zA-Z0-9\u4e00-\u9fa5]{1,7}$/
        if (reg.test(this.inputUserName) === true) {
          // 通过了校验
          await updateUserProfileAPI({
            name: this.inputUserName
          })
          this.profileObj.name = this.inputUserName
          done()
        } else {
          Notify({ type: 'warning', message: '用户名1-7位中英数字组合' })
          done(false)
        }
      } else {
        // 点击的是取消
        done() // 关闭弹窗
      }
    },
    // 生日单元格点击事件
    birthdayClickFn () {
      // 让时间选择器出现
      this.dateTimeShow = !this.dateTimeShow
      // 数据页面显示->年-月-日格式字符串
      this.currentDate = new Date(this.profileObj.birthday)
    },
    // 日期选择器取消事件
    dataTimeCancelFn () {
      this.dateTimeShow = !this.dateTimeShow
    },
    // 点击确定事件
    async confirmFn () {
      await updateUserProfileAPI({
        birthday: formatDate(this.currentDate)
      })
      this.dateTimeShow = !this.dateTimeShow
      this.profileObj.birthday = formatDate(this.currentDate)
    }
  }
}
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
/deep/ .van-dialog__content {
  text-align: center;
  input {
    padding: 0;
    outline: none;
    border: none;
    text-align: center;
  }
}
</style>
